fix: move logging configuration from MCPServer.__init__ to run()#2285
Open
omar-y-abdi wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: move logging configuration from MCPServer.__init__ to run()#2285omar-y-abdi wants to merge 1 commit intomodelcontextprotocol:mainfrom
omar-y-abdi wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
MCPServer.__init__() called configure_logging() which invokes logging.basicConfig(), configuring the root logger with handlers and level on every instantiation. This violates Python's logging best practice for libraries: library code must never configure logging — only application entrypoints should. Move configure_logging() to run(), which is the actual application entrypoint. MCPServer used as a library no longer has logging side effects. Github-Issue: modelcontextprotocol#1656 Reported-by: SimonZehetner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
MCPServer.__init__()callsconfigure_logging()which invokeslogging.basicConfig()— configuring the root logger with handlers and a level on every instantiation. Per Python's logging documentation:Any application that imports and instantiates
MCPServergets its logging configuration silently overwritten.Fix
Move the single
configure_logging(self.settings.log_level)call from__init__torun(). This is the actual application entrypoint — the method users call when they want to start the server as a standalone process.MCPServer("test")(library usage) — zero logging side effectsmcp.run()(standalone entrypoint) — configures logging before startingChanges
src/mcp/server/mcpserver/server.py— moved 1 call (3 lines removed, 2 added)tests/server/mcpserver/test_server.py— regression test verifying__init__does not callconfigure_loggingFull test suite passes with 100% coverage.
Closes #1656